package geppetto.moses;

import java.io.PrintStream;

public class MosesIni {	
	protected String[] phraseTables;
	protected String[] reorderingTables;
	protected String[] languageModels;
	protected int numberOfTranslationWeights;
	protected int numberOfReorderingWeights;	
	
	public MosesIni(String[] phraseTables, String[] reorderingTables,
			String[] languageModels, int numberOfTranslationWeights,
			int numberOfReorderingWeights) {
		super();
		this.phraseTables = phraseTables;
		this.reorderingTables = reorderingTables;
		this.languageModels = languageModels;
		this.numberOfTranslationWeights = numberOfTranslationWeights;
		this.numberOfReorderingWeights = numberOfReorderingWeights;
	}
	
	public MosesIni(String translationTable, String languageModel,
			String reorderingModel, int numberOfTranslationWeights,
			int numberOfReorderingWeights) {
		super();
		this.phraseTables = new String[]{translationTable};
		this.languageModels = new String[]{languageModel};
		this.reorderingTables = new String[]{reorderingModel};
		this.numberOfTranslationWeights = numberOfTranslationWeights;
		this.numberOfReorderingWeights = numberOfReorderingWeights;
	}

	public void print(PrintStream out){
		out.println(stringHeader());
		out.println(stringDummyInputFactors());
		out.println(stringDummyMappingSteps(phraseTables, reorderingTables));
		out.println(stringTranslationModel(phraseTables,numberOfTranslationWeights));
		out.println(stringDummyGenerationModels());
		out.println(stringLanguageModel(languageModels));
		out.println(stringNumberOfLoadedTranslations(20));
		out.println(stringDummyReorderingModel(reorderingTables));
		out.println(stringDummyReorderingWeights(reorderingTables, numberOfReorderingWeights));
		out.println(stringDummyLanguageModelWeight(languageModels));
		out.println(stringDummyTranslationModelWeight(phraseTables, numberOfTranslationWeights));
		out.println(stringDummyGenerationModelsWeight());
		out.println(stringWordPenalty(-1));
		out.println(stringDistortionLimit(6));
	}
	
	protected static String stringHeader(){
		return "#########################\n" +
		"### MOSES CONFIG FILE ###\n" +
		"#########################\n";

	}
	
	protected static String stringDummyInputFactors(){
		return "# input factors\n" +
			"[input-factors]\n" +
			"0\n";
	}
	
	protected static String stringDummyMappingSteps(String[] phraseTables, String[] reorderingTables){
		String ret = "# mapping steps\n" +
			   "[mapping]\n";
		for(int i = 0; i < phraseTables.length; i++){
			ret+= i + " T " + i + "\n";
		}
		return ret;
	}
	
	protected static String stringTranslationModel(String[] phraseTables, int numberOfTranslationWeights){
		String ret = "# translation tables: source-factors, target-factors, number of scores, file\n" +
				"[ttable-file]\n";
		for(int i = 0; i < phraseTables.length; i++){
			ret += "0 0 0 " + numberOfTranslationWeights + " " + phraseTables[i] + "\n";
		}
		return ret;
	}
	
	protected static String stringDummyGenerationModels(){
		return "# no generation models, no generation-file section\n";
	}
	
	protected static String stringLanguageModel(String[] langModels){
		String ret = "# language models: type(srilm/irstlm), factors, order, file\n" +
				"[lmodel-file]\n";
		for(int i = 0; i < langModels.length; i++){
			ret += "0 0 5 " + langModels[i] + "\n";
		}
		return ret;				
	}
	
	protected static String stringNumberOfLoadedTranslations(int n){
		return "# limit on how many phrase translations e for each phrase f are loaded\n" +
				"# 0 = all elements loaded\n" +
				"[ttable-limit]\n" +
				n + "\n";
	}
	
	protected static String stringDummyReorderingModel(String[] reorderingTables){
		String ret = "# distortion (reordering) files\n" +
				"[distortion-file]\n";
		for(int i = 0; i < reorderingTables.length; i++){
			ret += "0-0 msd-bidirectional-fe 6 " + reorderingTables[i] + "\n";
		}
		return ret;		
	}
	
	protected static String stringDummyReorderingWeights(String[] reorderingTables, int numberOfReorderingWeights){
		String ret = "# distortion (reordering) weight\n" +
		"[weight-d]\n0.3\n";
		for(int i = 0; i < reorderingTables.length; i++){
			for(int j = 0; j < numberOfReorderingWeights; j++){
				ret += "0.3\n";
			}
		}
		return ret;
	}
	
	protected static String stringDummyLanguageModelWeight(String[] languageModel){
		String ret = "# language model weights\n" +
		"[weight-l]\n";
		for(int i = 0; i < languageModel.length; i++){
			ret += "0.5000\n";
		}
		return ret;
	}
	
	protected static String stringDummyTranslationModelWeight(String[] phraseTables, int numberOfTranslationWeights){
		String ret = "# translation model weights\n" +
				"[weight-t]\n";
		for(int i = 0; i < phraseTables.length; i++){
			for(int j = 0; j < numberOfTranslationWeights; j++){
				ret += 	"0.2\n";				
			}
		}
		return ret;

	}
	
	protected static String stringDummyGenerationModelsWeight(){
		return "# no generation models, no weight-generation section\n";
	}
	
	protected static String stringWordPenalty(int penalty){
		return "# word penalty\n" +
				"[weight-w]\n" +
				penalty + "\n";
	}
	
	protected static String stringDistortionLimit(int limit){
		return "[distortion-limit]\n" +
				limit+"\n";
	}
}
